// pages/community/detail/detail.js
var common = require('../../../utils/common.js')
var util = require('../../../utils/util.js')

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {},
    discussImage: [],
    replayList: [],
    replayCount: 0,
    showSend: false,
    newreplaycontent: "", 
    offline: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id;//帖子id
    const accountid = app.globalData.accountid;
    const that = this;

    const url = "/apid/r/109";
    const imgurl = "/apid/r/110";
    const params = "id=" + id + "&accountid=" + accountid;
    const imgparams = "id=" + id;
    //获取帖子详情
    util.DoCommand(url, params, null, function (data) {
      that.setData({
        item: data,
      });
    }, null, that)
    //获取帖子图片
    var discussImage = [];
    util.DoCommand(imgurl, imgparams, null, function (imgdata) {
      for (var i in imgdata) {
        discussImage.push(imgdata[i].imgUrl)
      }
      that.setData({
        discussImage: discussImage,
      });
    }, null, that)
    //获取帖子评论
    this.getReply(id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // const id = this.data.item.id;//帖子id
    // console.log(this.data.item)
    // const newid = util.NewGuid();
    // const  url = "/apid/c/113?discussid=" + id + "&accountid=" + accountid + "&id=" + newid;
    // //调用接口记录阅读状态
    // util.DoCommand(url, null, null, function (data) { })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    const that=this;
    //记录分享次数
    var shareObj = {
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
          const accountid = app.globalData.accountid;
          const discussid = that.data.item.id
          const id = util.NewGuid();
          const url = "/apid/c/132";
          const params = "discussid=" + discussid + "&accountid=" + accountid + "&id=" + id;
          util.DoCommand(url, params, null, function (data) { }, null, that)
        }

      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      },
      complete: function () {
        // 转发结束之后的回调（转发成不成功都会执行）
      }
    }
    return shareObj;
  },
  //获取帖子评论
  getReply: function (id) {
    const that = this
    const replyurl = "/apid/r/114";
    const params = "id=" + id;
    util.DoCommand(replyurl, params, null, function (data) {
      that.setData({
        replayList: data,
        replayCount: data.length
      });
      //更改上个页面（列表页）的评论数
      var pages = getCurrentPages();
      if (pages.length > 1) {
        //上一个页面实例对象
        var prePage = pages[pages.length - 2];
        //改更列表页的状态
        prePage.changeData(id, 'none', 'none', data.length);
      }
    }, null, that)

  },
  //预览图片
  previewImage: function (e) {
    const currentSrc = e.currentTarget.dataset.src;
    const imgList = this.data.discussImage;
    wx.previewImage({ 
      current: currentSrc, // 当前显示图片的http链接  
      urls: imgList // 需要预览的图片http链接列表  
    })
  },
  //收藏
  tapFavorite: function (e) {
    const that = this;
    const isfavorite = e.currentTarget.dataset.isfavorite;
    const currentData = that.data.item;
    const discussid = currentData.id;
    const accountid = app.globalData.accountid;
    var newfavorite = 0;
    var url = "";
    var params=null;
    //如果已收藏则将收藏状态改为为收藏
    if (isfavorite == 1) {
      //取消收藏 
      newfavorite = 0;
      currentData.isfavorite = newfavorite;
      url = "/apip/discuss/nofavorite/" + discussid + "/" + accountid
    }
    //如果未收藏则将收藏状态改为已收藏
    else {
      //收藏 
      newfavorite = 1;
      currentData.isfavorite = newfavorite;
      const id = util.NewGuid();
      url = "/apid/c/111";
      params = "discussid=" + discussid + "&accountid=" + accountid + "&id=" + id
    }

    //调用接口记录收藏状态
    util.DoCommand(url, params, null, function (data) {
      //更改本页收藏状态
      that.setData({
        item: currentData
      })
      //更改列表页的收藏状态
      var pages = getCurrentPages();
      if (pages.length > 1) {
        //上一个页面实例对象
        var prePage = pages[pages.length - 2];
        //改更列表页的状态
        prePage.changeData(discussid, 'none', newfavorite, 'none');
      }

    }, null, that)
  },
  //点赞
  tapSupport: function (e) {
    const that = this;
    const issupport = e.currentTarget.dataset.issupport;
    const accountid = app.globalData.accountid;
    const currentData = this.data.item;
    const discussid = currentData.id;
    var newissupport = 0;
    var url = "";
    var params = null;
    //如果已支持则将收藏状态改为未支持
    if (issupport == 1) {
      //取消点赞
      newissupport = 0;
      currentData.issupport = newissupport;
      url = "/apip/discuss/nosupport/" + discussid + "/" + accountid
    }
    //如果未支持则将收藏状态改为已支持
    else {
      //点赞
      newissupport = 1;
      currentData.issupport = newissupport;
      const id = util.NewGuid();
      url = "/apid/c/112";
      params = "discussid=" + discussid + "&accountid=" + accountid + "&id=" + id;
    }
    //调用接口记录点赞状态
    util.DoCommand(url, params, null, function (data) {
      that.setData({
        item: currentData
      });

      //更改列表页的点赞状态
      var pages = getCurrentPages();
      if (pages.length > 1) {
        //上一个页面实例对象
        var prePage = pages[pages.length - 2];
        //改更列表页的状态
        prePage.changeData(discussid, newissupport, 'none', 'none');
      }

    }, null, that)

  },
  //点击发送按钮--提交评论
  formSubmit: function (e) {
    this.submitReplay(e.detail.value.content);
  },
  //点击键盘的发送按钮-提交评论
  inputSubmit: function (e) {
    this.submitReplay(e.detail.value);
  },
  //评论框聚焦时，显示发送按钮
  inputFocus: function (e) {
    this.setData({
      showSend: true
    })
  },
  //评论框失焦时，隐藏发送按钮并且清空内容
  inputblur: function (e) {
    this.setData({
      showSend: false,
      newreplaycontent: ''
    })
  },
  submitReplay(content) {
    const that = this;
    const id = util.NewGuid();
    var data = {};
    const discussid = this.data.item.id;
    data.content = content;
    data.accountid = app.globalData.accountid;;
    data.discussid = discussid;
    //TODO 敏感词过滤
    var url = "/apid/c/115";
    util.DoCommand(url, "id=" + id, data, function (data) {
      //重新加载帖子评论
      that.getReply(discussid);
    }, null, that)
  }
})